﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;

namespace EdexApiClient
{
    public partial class EdexApiContext
    {
        /// <summary>
        /// Получить исходящие накладные
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Invoices> GetOutgoingInvoices()
        {
            return this.GetOutgoingInvoices(String.Empty);
        }

        /// <summary>
        /// Получить исходящие накладные
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public IEnumerable<Invoices> GetOutgoingInvoices(string search)
        {
            var uri = String.Format("OutgoingInvoices/?api_key={0}", this.session.APIKEY);

            //if (!String.IsNullOrEmpty(search))
            //    uri += "&$filter=substringof('" + search + "', DOC_NUMBER) eq true&$top=100";
            if (!String.IsNullOrEmpty(search))
                uri += "&$filter=" + search;

            uri += "&$orderby=DOC_DATE";

            return this.GetData<List<Invoices>>(uri);
        }

        /// <summary>
        /// Получить незагруженные исходящие накладные
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Invoices> GetNewOutgoingInvoices()
        {
            var uri = String.Format("OutgoingInvoices/?api_key={0}", this.session.APIKEY);
            uri += "&$filter=CUSTOMER_DOC_ID eq null";
            uri += "&$orderby=DOC_DATE";

            return this.GetData<List<Invoices>>(uri);
        }

        /// <summary>
        /// Получить последние исходящие накладные
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Invoices> GetLastOutgoingInvoices()
        {
            var uri = String.Format("OutgoingInvoices/?api_key={0}", this.session.APIKEY);
            uri += "&$filter=CUSTOMER_DOC_ID eq null";
            uri += "&$orderby=DATECREATE desc";
            uri += "&$top=1";

            return this.GetData<List<Invoices>>(uri);
        }

        /// <summary>
        /// Получить детализацию по исходящей накладной
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Invoices GetOutgoingInvoice(string id)
        {
            return this.GetData<Invoices>(String.Format("OutgoingInvoices/{0}/?api_key={1}", id, this.session.APIKEY));
        }
        /// <summary>
        /// Получить детализацию по позиции в исходящей накладной
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public InvoiceLines GetOutgoingInvoiceLine(string id)
        {
            return this.GetData<InvoiceLines>(String.Format("OutgoingInvoiceLines/{0}/?api_key={1}", id, this.session.APIKEY));
        }

        /// <summary>
        /// Добавить детализацию по накладной
        /// </summary>
        /// <param name="data"></param>
        public void AddInvoiceDetails(InvoiceLines data)
        {
            var uri = String.Format("OutgoingInvoiceLines/?api_key={0}", this.session.APIKEY);
            this.Post<InvoiceLines>(uri, data);
        }

        /// <summary>
        /// Обновить детализацию по накладной
        /// </summary>
        /// <param name="data"></param>
        public void UpdateInvoiceDetails(InvoiceLines data)
        {
            var uri = String.Format("OutgoingInvoiceLines/?api_key={0}", this.session.APIKEY);
            this.Put<InvoiceLines>(uri, data);
        }

        /// <summary>
        /// Удалить детализацию по накладной
        /// </summary>
        /// <param name="id"></param>
        public void DeleteInvoiceDetails(string id)
        {
            var uri = String.Format("OutgoingInvoiceLines/{0}/?api_key={1}", id, this.session.APIKEY);
            this.Delete(uri);
        }

        /// <summary>
        /// Добавить накладную
        /// </summary>
        /// <param name="data"></param>
        public void AddInvoice(Invoices data)
        {
            var uri = String.Format("OutgoingInvoices/?api_key={0}", this.session.APIKEY);
            this.Post<Invoices>(uri, data);
        }

        /// <summary>
        /// Обновить накладную
        /// </summary>
        /// <param name="data"></param>
        public void UpdateInvoice(Invoices data)
        {
            var uri = String.Format("OutgoingInvoices/?api_key={0}", this.session.APIKEY);
            this.Put<Invoices>(uri, data);
        }

        /// <summary>
        /// Удалить накладную
        /// </summary>
        /// <param name="id"></param>
        public void DeleteInvoice(string id)
        {
            var uri = String.Format("OutgoingInvoices/{0}/?api_key={1}", id, this.session.APIKEY);
            this.Delete(uri);
        }

        public async Task<HttpResponseMessage> UploadOutgoingInvoices(System.IO.Stream stream, string fileName)
        {
            var uri = String.Format("OutgoingInvoicesCommerceML/?api_key={0}", this.session.APIKEY);
            return await this.Upload(uri, stream, fileName);
        }

        public HttpResponseMessage UploadOutgoingInvoicesSync(System.IO.Stream stream, string fileName)
        {
            var uri = String.Format("OutgoingInvoicesCommerceML/?api_key={0}", this.session.APIKEY);
            return this.UploadSync(uri, stream, fileName);
        }
    }

    public class ExceptionInfo
    {
        public string Message { get; set; }
        public string ExceptionMessage { get; set; }
        public string ExceptionType { get; set; }
        public string StackTrace { get; set; }
    }
}